package com.ruoyi.system.service.impl;

import java.util.List;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysTextbook;
import com.ruoyi.system.mapper.SysTextbookMapper;
import com.ruoyi.system.service.ISysTextbookService;

/**
 * 管理Service业务层处理
 * 
 * @author Jason
 */
@Service
public class SysTextbookServiceImpl implements ISysTextbookService 
{
    
    @Autowired
    private SysTextbookMapper textbookMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询
     * 
     * @param textbookId ID
     * @return 
     */
    @Override
    public SysTextbook selectById(Long textbookId)
    {
        return textbookMapper.selectById(textbookId);
    }

    /**
     * 查询列表
     * 
     * @param entity 
     * @return 
     */
    @Override
    public List<SysTextbook> selectList(SysTextbook entity)
    {
        return textbookMapper.selectList(entity);
    }

    /**
     * 新增
     * 
     * @param entity 
     * @return 结果
     */
    @Override
    public int insert(SysTextbook entity)
    {
        return textbookMapper.insert(entity);
    }

    /**
     * 修改
     * 
     * @param entity 
     * @return 结果
     */
    @Override
    public int update(SysTextbook entity)
    {
        return textbookMapper.update(entity);
    }

    /**
     * 批量删除
     * 
     * @param textbookIds 需要删除的ID
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] textbookIds)
    {
        return textbookMapper.deleteByIds(textbookIds);
    }

    /**
     * 删除信息
     * 
     * @param textbookId ID
     * @return 结果
     */
    @Override
    public int deleteById(Long textbookId)
    {
        return textbookMapper.deleteById(textbookId);
    }

    /**
     * 导入数据
     * 
     * @param listEntity 数据列表
     * @return 结果
     */
    @Override
    public String importData(List<SysTextbook> listEntity)
    {
        if (StringUtils.isNull(listEntity) || listEntity.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysTextbook entity : listEntity)
        {
            try
            {
                BeanValidators.validateWithException(validator, entity);
                textbookMapper.insert(entity);
                successNum++;
                successMsg.append("<br/>" + successNum + "、教材名称 " + entity.getTextbookName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                e.printStackTrace();
//                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}